from flask import Flask
from flask import render_template
from jieba.analyse import extract_tags
from flask import jsonify
import mysql_to_back

app = Flask(__name__)


@app.route('/')
def main():
    return render_template('main.html')


@app.route('/time')
def get_time():
    return mysql_to_back.get_time()


@app.route('/c1')
def get_c1_data():
    data = mysql_to_back.get_c1_data()
    return jsonify({"confirm_total": int(data[0]), "suspect_total": int(data[1]),
                    "heal_total": int(data[2]), "dead_total": int(data[3])})


@app.route('/c2')
def get_c2_data():
    res = []
    for item in mysql_to_back.get_c2_data():
        res.append({'name': item[0], 'value': int(item[1])})
    return jsonify({'information': res})


@app.route('/l1')
def get_l1_data():
    data = mysql_to_back.get_l1_data()
    day, confirm, suspect, heal, dead = [], [], [], [], []
    for a, b, c, d, e in data[-1::-1]:
        day.append(a.strftime('%m-%d'))
        confirm.append(b)
        suspect.append(c)
        heal.append(d)
        dead.append(e)
    return jsonify({'day': day, 'confirm': confirm, 'suspect': suspect, 'heal': heal, 'dead': dead})


@app.route('/l2')
def get_l2_data():
    data = mysql_to_back.get_l2_data()
    day, confirm_add, suspect_add, heal_add, dead_add = [], [], [], [], []
    for a, b, c, d, e in data[-1::-1]:
        day.append(a.strftime('%m-%d'))
        confirm_add.append(b)
        suspect_add.append(c)
        heal_add.append(d)
        dead_add.append(e)
    return jsonify({'day': day, 'confirm_add': confirm_add, 'suspect_add': suspect_add,
                    'heal_add': heal_add, 'dead_add': dead_add})


@app.route('/r1')
def get_r1_data():
    data = mysql_to_back.get_r1_data()
    city, confirm = [], []
    for k, v in data:
        city.append(k)
        confirm.append(int(v))
    return jsonify({'city': city, 'confirm': confirm})


@app.route('/r2')
def get_r2_data():
    data = mysql_to_back.get_r2_data()
    tar = []
    for k, v in data:
        key_word = extract_tags(k)
        for j in key_word:
            tar.append({'name': j, 'value': v})
    return jsonify({'kws': tar})


if __name__ == '__main__':
    app.run()
